#include "PlayMenuState.h"

#include "Game.h"
#include "GamePlayState.h"
#include "MainMenuState.h"
#include "LevelMenuState.h"
#include "BitmapFont.h"

#include "AudioManager.h"
//accessing cur profile
#include "ProfileManager.h"


PlayMenuState* PlayMenuState::pInstance = nullptr;
/*static*/ PlayMenuState* PlayMenuState::GetInstance( void )
{
	// Allocate the static pointer if necessary
	if( pInstance == nullptr )
		pInstance = new PlayMenuState;

	// Return the singleton
	return pInstance;
}
/*static*/ void PlayMenuState::DeleteInstance( void )
{
	delete pInstance;
	pInstance = nullptr;
}


// Update
void PlayMenuState::Update(float deltaTime)
{
	if(controllerDelay > 0.0f)
		controllerDelay -= deltaTime;
	if(dI->KeyPressed(DIK_ESCAPE) )
	{
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
		return;
	}
	if(ProfileManager::GetInstance()->getCurProfile().getControlScheme() == 5)
	{
		if(dI->JoystickButtonPressed(6))
		{
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
			return;
		}
	}
	if(dI->KeyPressed(DIK_RETURN) || dI->JoystickButtonPressed(0))
	{
		switch(cursor)
		{
		case 0:
			{
				//Tell Levelmanager which level to load
				int curLevel = ProfileManager::GetInstance()->getCurProfile().getLevelsUnlocked();
				if(curLevel > 24)
					curLevel = 24;
				//Load up proper level
				GamePlayState::GetInstance()->Enter(curLevel);
				GamePlayState::GetInstance()->GetlevelManager().SetCurLevel(curLevel);
				Game::GetInstance()->ChangeState(GamePlayState::GetInstance());
				if(curLevel > 19)
				{
					AudioManager::GetInstance()->switchMusic("menus", "menus");
				}
				else if( curLevel > 9)
				{
					AudioManager::GetInstance()->switchMusic("menus", "gameplay2");
				}
				else
				{
					AudioManager::GetInstance()->switchMusic("menus","gameplay");
				}
				break;
			}
		case 1:
			Game::GetInstance()->ChangeState(LevelMenuState::GetInstance());
			break;
		case 2:
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
			break;
		}
		return;
	}
	if(dI->KeyPressed(DIK_UP) || dI->KeyPressed(DIK_W) || dI->JoystickDPadPressed(DIR_UP) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.17f;
		if(cursor > 0)
			--cursor;
		else
			cursor = 2;
	}
	else if (dI->KeyPressed(DIK_DOWN) || dI->KeyPressed(DIK_S) || dI->JoystickDPadPressed(DIR_DOWN) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.25f;
		if(cursor < 2)
			++cursor;
		else
			cursor = 0;
	}
}

// Render
void PlayMenuState::Render(void) const
{
	if( BackgroundImageID != -1 )
	{
		tM->Draw(BackgroundImageID, 0,0,0.95f,1.055f);
	}

	//Get Draw Pos
	int ScreenCenterX = Game::GetInstance()->GetScreenWidth() / 2;
	int ScreenCenterY = Game::GetInstance()->GetScreenHeight() / 2;

	int DrawXPos = ScreenCenterX - 208;
	int DrawYPos = ScreenCenterY - 274;

	switch(cursor)
	{
	case 0:
		{
			if( BackButton != -1 )
			{
				tM->Draw(BackButton,DrawXPos,DrawYPos,0.5f,0.5f);
			}
			break;
		}
	case 1:
		{
			if( LevelSelectButton != -1 )
			{
				tM->Draw(LevelSelectButton, DrawXPos,DrawYPos,0.5f,0.5f);
			}
			break;
		}
	case 2:
		{
			if( ContinueButton != -1 )
			{
				tM->Draw(ContinueButton,DrawXPos,DrawYPos,0.5f,0.5f);
			}
			break;
		}
	}

	//for(int i =0; i < 2; ++i)
	//{
	//	renderRect.top	 = 100 + 60*i + 10*i;
	//	renderRect.bottom = 160 + 60*i + 10*i;
	//	renderRect.left	 = 300; renderRect.right = 500;

	//	D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,255,0,0));
	//	switch(i)
	//	{
	//	case 0:
	//		BitmapFont::GetInstance()->Print("Continue", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,0,0,0));
	//		break;
	//	case 1:
	//		BitmapFont::GetInstance()->Print("Level Select", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,0,0,0));
	//		break;
	//	}
	//}
	//renderRect.top = Game::GetInstance()->GetScreenHeight()-100;
	//renderRect.bottom = Game::GetInstance()->GetScreenHeight()-20;
	//renderRect.left = 5; renderRect.right = 200;
	//D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,255,0,0));
	//BitmapFont::GetInstance()->Print("Back", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,0,0,0));


	//RECT selectedRect;
	//if(cursor < 2)
	//{
	//	selectedRect.top = 100 + 60*cursor + 10*cursor;
	//	selectedRect.bottom = 160 + 60*cursor + 10*cursor;
	//	selectedRect.left = 300; selectedRect.right = 500;
	//}
	//else
	//{
	//	selectedRect.top = Game::GetInstance()->GetScreenHeight()-100;
	//	selectedRect.bottom = Game::GetInstance()->GetScreenHeight()-20;
	//	selectedRect.left = 5; selectedRect.right = 200;
	//}
	//D3D->DrawHollowRect(selectedRect,D3DCOLOR_ARGB(255,255,255,0),3);
}

// Enter
//	- Used to initialize the game state.
void PlayMenuState::Enter(void)
{
	GameState::Enter();

	BackgroundImageID = tM->LoadTexture("Textures\\Menu\\LevelSelectBackground.jpg");
	BackButton = tM->LoadTexture("Textures\\Menu\\PlayMenuContinue.png");
	LevelSelectButton = tM->LoadTexture("Textures\\Menu\\PlayMenuLevelSelect.png");
	ContinueButton = tM->LoadTexture("Textures\\Menu\\PlayMenuBack.png");
}

// Exit
//	- Used to clean up the game state.
void PlayMenuState::Exit(void)
{
	GameState::Exit();
	DeleteInstance();
}


PlayMenuState::PlayMenuState(void)
{
	cursor = 0;

	BackgroundImageID = -1;
	BackButton = -1;
	LevelSelectButton = -1;
	ContinueButton = -1;
}
PlayMenuState::~PlayMenuState(void)
{

}